package com.bonana.backend.controller;

import com.bonana.core.entity.CoreUser;
import com.bonana.core.entity.CoreUserProfile;
import com.bonana.core.entity.mybatis.Platform;
import com.bonana.core.entity.mybatis.Project;
import com.bonana.core.entity.mybatis.UserAlbum;
import com.bonana.core.form.UserSingleFieldFrom;
import com.bonana.core.result.Response;
import com.bonana.core.result.ResponseCode;
import com.bonana.core.services.AlbumService;
import com.bonana.core.services.BoBoService;
import com.bonana.core.services.ProjectService;
import com.bonana.core.services.UtilsService;
import com.bonana.core.vo.BoPlanVo;
import com.bonana.utils.ConvertUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/7/5.
 */
@Controller
@RequestMapping(value="index")
public class IndexController {
    @Autowired
    BoBoService boBoService;
    @Autowired
    ProjectService projectService;
    @Autowired
    UtilsService utilsService;
    @Autowired
    private HttpSession session;
    @Autowired
    private AlbumService albumService;

    @RequestMapping(value="project")
    public String IndexProject(Model model) {
        List<Project> indexProj = projectService.getIndexProjs();
        model.addAttribute("proj", indexProj);
        return "index/project";
    }

    @RequestMapping(value="bobo")
    public String IndexBobo(Model model) {
        List<CoreUserProfile> indexProj = boBoService.getIndexBobos(10000);
        model.addAttribute("bobo", indexProj);
        return "index/bobo";
    }

    private int getCurUid() {
        int uid = 0;
        if (session.getAttribute("session.uid") != null) {
            uid = ConvertUtils.toInteger(session.getAttribute("session.uid"));
        }
        return uid;
    }
    @RequestMapping(value="boboh5")
    public String BoboH5(Model model) {
        List<BoPlanVo> boplans = boBoService.getBoboPlanDisplay(getCurUid());
        model.addAttribute("boplans", boplans);
        List<Platform> platforms = utilsService.getPlatform();
        model.addAttribute("platforms", platforms);
        CoreUserProfile profile = boBoService.queryUserProfileByUid(getCurUid(), (long) 0);
        model.addAttribute("profile", profile);
        return "index/boboh5";
    }

    @RequestMapping(value = "saveProfile", method = RequestMethod.POST)
    @ResponseBody
    public Response saveUserProfile(UserSingleFieldFrom fieldFrom) {
        if (boBoService.updateSingleField(getCurUid() , fieldFrom) > 0) {
            return new Response(ResponseCode.SUCC);
        } else {
            return new Response(ResponseCode.ERROR);
        }
    }

    @RequestMapping(value = "album" , method = RequestMethod.POST)
    @ResponseBody
    public UserAlbum updateAlbum(UserAlbum album) {
        Integer uid = 0;
        if (session.getAttribute("session.uid") != null) {
            uid = ConvertUtils.toInteger(session.getAttribute("session.uid"));
            album.setUserId(uid);
            if (album.getAlbumId()!=0) {
                albumService.RemoveAlbumById(album.getAlbumId());
            } else {
                album.setCreateTime(new Date());
                int ret = albumService.AddAlbum(album);
                album.setAlbumId(ret);
            }
        }
        return album;
    }

    @RequestMapping(value = "getUserAlbum" , method = RequestMethod.POST)
    @ResponseBody
    public PageInfo getUserAlbum(Integer uid, int pgSize, int pgNum) {
        List<UserAlbum> albums = albumService.getUserAlbum(uid,pgSize,pgNum);
        return new PageInfo(albums);
    }

}
